import React,{Component} from 'react';
import {
  Link
} from 'react-router-dom';
import { getAnnouncement } from 'api/home.js';
import './news.scss';
export default class News extends Component{
	constructor(props){
    super(props)
    this.state = {
      content:[],
      currentList:['公告', '新闻'],
      current:0,
      currentPage: 0,
      lastPage: 0
    }
	}
	click1 (index) {
		this.setState({
			current:index
		}, ()=>{
			this._getAnnouncement(index+1,1)
		})
	}
	_getAnnouncement (sum,num) { // 获取首页公告或者新闻列表接口
		getAnnouncement({
			sort:sum,
			page:num
		}).then(res => {
			if (res.code === 1) {
				this.setState({
					content:[]
				},() => {
					this.setState({
	         content: this.state.content.concat(res.data.data)
	        })
				})
        
      }
		})
	}
	componentWillMount () {
		if(this.props.location.state){
			this.click1(this.props.location.state.current,1)
		} else {
			this._getAnnouncement(1,1);
		}
		
		let that=this;
    let i=1;
    window.onscroll= function(){
      var t = document.documentElement.scrollTop||document.body.scrollTop;
      var b = document.documentElement.scrollHeight||document.body.scrollHeight;
      var h = document.documentElement.clientHeight||document.body.clientHeight;
      if((+t+h) == b && i <= that.state.lastPage && t!==0){
        that._getAnnouncement(1,i++)
      }
    }
	}
	render(){
		return(
			<div id="news">
				<div className="news_title"><Link to="/"><span className="glyphicon glyphicon-back back"></span></Link>资讯</div>
				<div className="news_box">
				{
					this.state.currentList.map((item,index) => {
						return(
							<span onClick={this.click1.bind(this,index)} className={this.state.current === index && 'active1'} key={index}>{item}</span>
						)
					})
				}
				</div>
				<div id="gongg">
				{
					this.state.content.map((item,index) => {
						return(
							<Link to={
		            {
		              pathname: '/news/notice',
		              state:{
		              	article_id: item.article_id,
		              	current: this.state.current
		              }
		            }
		          } key={index}>
		            <p>{item.article_title}</p>
		            <span>{item.article_add_time}</span>
		          </Link>
						)
					})
				}
				</div>
      </div>
		)
	}
}